﻿'*******************************************************************************************
'* Copyright Brügmann Software GmbH, Papenburg
'* Author: Björn Kremer
'* Contact: codeplex<at>patorg.de
'* 
'* VB.CMIS is a VB.NET implementation of the Content Management Interoperability Services (CMIS) standard
'*
'* This file is part of VB.CMIS.
'* 
'* VB.CMIS is free software: you can redistribute it and/or modify
'* it under the terms of the GNU Lesser General Public License as published by
'* the Free Software Foundation, either version 3 of the License, or
'* (at your option) any later version.
'* 
'* VB.CMIS is distributed in the hope that it will be useful,
'* but WITHOUT ANY WARRANTY; without even the implied warranty of
'* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
'* GNU Lesser General Public License for more details.
'* 
'* You should have received a copy of the GNU Lesser General Public License
'* along with VB.CMIS. If not, see <http://www.gnu.org/licenses/>.
'*******************************************************************************************
Imports System.Runtime.Serialization
Imports System.ServiceModel.Syndication
Imports System.Xml.Serialization
Imports CMISObjectModel.TypeDefinitions

Namespace ServiceDocument


   <XmlRoot("repositoryInfo", Namespace:=CmisNamespaceConstants.Cmisra)> _
   Public Class RepositoryInfo
      Implements IXmlSerializable

      Public Property RepositoryId() As String

      Public Property RepositoryName() As String

      Public Property RepositoryDescription() As String

      Public Property VendorName() As String

      Public Property ProductName() As String

      Public Property ProductVersion() As String

      Public Property RootFolderId() As String

      Public Property LatestChangeLogToken() As String

      Public Property Capabilities() As RepositoryCapabilities

      Public Property AclCapability() As AclCapability

      Public Property CmisVersionSupported() As String

      Public Property ThinClientURI() As String

      Public Property ChangesIncomplete() As Boolean

      Public Property ChangesOnType() As TypeDefinition.BaseIdEnum()

      Public Property PrincipalAnonymous() As String

      Public Property PrincipalAnyone() As String

      Public Property ExtendedFeatures As ExtensionFeatureType()


      Public Function GetAtomLinks(baseUri As Uri) As List(Of CMISLink)
         Dim retVal As New List(Of CMISLink)

         retVal.Add(New CMISLink(New Uri(baseUri, ServiceURIs.GetTypeDescendantsBase.Replace("{repositoryId}", RepositoryId).TrimStart("/"c)), CmisAtomLinkConstants.TypeDescendants, CmisContentTypeConstants.Feed))
         If Capabilities.GetFolderTree Then
            retVal.Add(New CMISLink(New Uri(baseUri, ServiceURIs.GetFolderTreeBase.Replace("{repositoryId}", RepositoryId).TrimStart("/"c)), CmisAtomLinkConstants.FolderTree, CmisContentTypeConstants.Tree))
         End If
         If Capabilities.GetDescendants Then
            retVal.Add(New CMISLink(New Uri(baseUri, ServiceURIs.GetDescendantsBase.Replace("{repositoryId}", RepositoryId).TrimStart("/"c)), CmisAtomLinkConstants.RootDescendants, CmisContentTypeConstants.Tree))
         End If
         If Capabilities.Changes <> RepositoryCapabilities.CapabilityChanges.None Then
            retVal.Add(New CMISLink(New Uri(baseUri, ServiceURIs.GetContentChangesBase.Replace("{repositoryId}", RepositoryId).TrimStart("/"c)), CmisAtomLinkConstants.Changes, CmisContentTypeConstants.Feed))
         End If
         Return retVal
      End Function

      Public Function GetUriTemplates(baseUri As Uri) As List(Of CMISUriTemplate)
         Dim objectByIdTemplate As String = ServiceURIs.GetObjectBase3.Replace("{repositoryId}", RepositoryId).TrimStart("/"c)
         Dim objectByPathTemplate As String = ServiceURIs.GetObjectByPath.Replace("{repositoryId}", RepositoryId).TrimStart("/"c)
         Dim typeByIdTemplate As String = ServiceURIs.GetTypeDefinition.Replace("{repositoryId}", RepositoryId).TrimStart("/"c)
         Dim queryTemplate As String = ServiceURIs.Query.Replace("{repositoryId}", RepositoryId).TrimStart("/"c)

         Dim cmisUriTemplates As New List(Of CMISUriTemplate)


         cmisUriTemplates.Add(New CMISUriTemplate(UriCombine(baseUri, objectByIdTemplate), "objectbyid", CmisContentTypeConstants.Entry))
         cmisUriTemplates.Add(New CMISUriTemplate(UriCombine(baseUri, objectByPathTemplate), "objectbypath", CmisContentTypeConstants.Entry))
         cmisUriTemplates.Add(New CMISUriTemplate(UriCombine(baseUri, typeByIdTemplate), "typebyid", CmisContentTypeConstants.Entry))

         If Capabilities.Query <> RepositoryCapabilities.CapabilityQuery.None Then
            cmisUriTemplates.Add(New CMISUriTemplate(UriCombine(baseUri, queryTemplate), "query", CmisContentTypeConstants.Feed))
         End If


         Return cmisUriTemplates
      End Function
      Private Shared Function UriCombine(uri1 As Uri, uri2 As String) As String
         Dim strUri As String = uri1.AbsoluteUri
         Return strUri & uri2
      End Function

      Public Function GetCollections(baseUri As Uri) As List(Of ResourceCollectionInfo)
         Dim collections As New List(Of ResourceCollectionInfo)


         Dim rootCollection As New ResourceCollectionInfo("Root folder collection", New Uri(baseUri, ServiceURIs.GetChildrenBase1.Replace("{repositoryId}", RepositoryId).Replace("{id}", RootFolderId).TrimStart("/"c)))
         rootCollection.ElementExtensions.Add("collectionType", CmisNamespaceConstants.Cmisra, "root")
         collections.Add(rootCollection)


         If Capabilities.Query <> RepositoryCapabilities.CapabilityQuery.None Then
            Dim queryCollection As New ResourceCollectionInfo("Query collection", New Uri(baseUri, ServiceURIs.QueryBase.Replace("{repositoryId}", RepositoryId).TrimStart("/"c)))
            queryCollection.Accepts.Add(CmisContentTypeConstants.Query)
            queryCollection.ElementExtensions.Add("collectionType", CmisNamespaceConstants.Cmisra, "query")
            collections.Add(queryCollection)
         End If


         Dim checkedOutCollection As New ResourceCollectionInfo("Checkedout collection", New Uri(baseUri, ServiceURIs.GetCheckedoutDocsBase.Replace("{repositoryId}", RepositoryId).TrimStart("/"c)))
         checkedOutCollection.Accepts.Add(CmisContentTypeConstants.Entry)
         checkedOutCollection.ElementExtensions.Add("collectionType", CmisNamespaceConstants.Cmisra, "checkedout")
         collections.Add(checkedOutCollection)


         If Capabilities.Unfiling = True Then
            Dim unfiledCollection As New ResourceCollectionInfo("Unfiled collection", New Uri(baseUri, ServiceURIs.RemoveObjectFromFolderBase.Replace("{repositoryId}", RepositoryId).TrimStart("/"c)))
            unfiledCollection.Accepts.Add(CmisContentTypeConstants.Entry)
            unfiledCollection.ElementExtensions.Add("collectionType", CmisNamespaceConstants.Cmisra, "unfiled")
            collections.Add(unfiledCollection)
         End If

         Dim bulkUpdateCollection As New ResourceCollectionInfo("Bulk update collection", New Uri(baseUri, ServiceURIs.BulkUpdate.Replace("{repositoryId}", RepositoryId).TrimStart("/"c)))
         bulkUpdateCollection.Accepts.Add(CmisContentTypeConstants.Entry)
         bulkUpdateCollection.ElementExtensions.Add("collectionType", CmisNamespaceConstants.Cmisra, "update")
         collections.Add(bulkUpdateCollection)

         Dim typecCollection As New ResourceCollectionInfo("Types children collection", New Uri(baseUri, ServiceURIs.GetTypeChildrenBase.Replace("{repositoryId}", RepositoryId).TrimStart("/"c)))
         typecCollection.ElementExtensions.Add("collectionType", CmisNamespaceConstants.Cmisra, "types")
         collections.Add(typecCollection)
         Return collections

      End Function

      Public Function GetSchema() As System.Xml.Schema.XmlSchema Implements System.Xml.Serialization.IXmlSerializable.GetSchema
         Return Nothing
      End Function

      Public Sub ReadXml(reader As System.Xml.XmlReader) Implements System.Xml.Serialization.IXmlSerializable.ReadXml

      End Sub

      Public Sub WriteXml(writer As System.Xml.XmlWriter) Implements System.Xml.Serialization.IXmlSerializable.WriteXml
         writer.WriteElementString("repositoryId", CmisNamespaceConstants.Cmis, RepositoryId)
         writer.WriteElementString("repositoryName", CmisNamespaceConstants.Cmis, RepositoryName)
         writer.WriteElementString("repositoryDescription", CmisNamespaceConstants.Cmis, RepositoryDescription)
         writer.WriteElementString("vendorName", CmisNamespaceConstants.Cmis, VendorName)
         writer.WriteElementString("productName", CmisNamespaceConstants.Cmis, ProductName)
         writer.WriteElementString("productVersion", CmisNamespaceConstants.Cmis, ProductVersion)
         writer.WriteElementString("rootFolderId", CmisNamespaceConstants.Cmis, RootFolderId)
         writer.WriteElementString("latestChangeLogToken", CmisNamespaceConstants.Cmis, LatestChangeLogToken)

         If Capabilities IsNot Nothing Then
            Dim serializer As New XmlSerializer(Capabilities.GetType)
            serializer.Serialize(writer, Capabilities)
         End If
         If AclCapability IsNot Nothing Then
            Dim serializer As New XmlSerializer(AclCapability.GetType)
            serializer.Serialize(writer, AclCapability)
         End If

         writer.WriteElementString("cmisVersionSupported", CmisNamespaceConstants.Cmis, CmisVersionSupported)
         writer.WriteElementString("thinClientURI", CmisNamespaceConstants.Cmis, ThinClientURI)
         writer.WriteElementString("changesIncomplete", CmisNamespaceConstants.Cmis, ChangesIncomplete.ToString.ToLowerInvariant)


         If ChangesOnType IsNot Nothing Then
            For Each val As TypeDefinition.BaseIdEnum In ChangesOnType
               writer.WriteElementString("changesOnType", CmisNamespaceConstants.Cmis, Common.GetDataMemberValueFromEnum(val, GetType(TypeDefinition.BaseIdEnum)))
            Next
         End If

         writer.WriteElementString("principalAnonymous", CmisNamespaceConstants.Cmis, PrincipalAnonymous)
         writer.WriteElementString("principalAnyone", CmisNamespaceConstants.Cmis, PrincipalAnyone)


         If ExtendedFeatures IsNot Nothing Then
            For Each feature As ExtensionFeatureType In ExtendedFeatures
               If feature IsNot Nothing Then
                  Dim serializer As New XmlSerializer(feature.GetType)
                  serializer.Serialize(writer, feature)
               End If
            Next
         End If
      End Sub
   End Class

End Namespace